package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Member;
import com.qy.model.Share;
import com.qy.service.MemberService;
import com.qy.service.ShareService;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zaq on 2018/12/12.
 */
@RestController
@RequestMapping("app/share")
public class ShareController {
    @Resource
    private ShareService shareService;

    @Resource
    private MemberService memberService;

    //分享关系关联
    @PostMapping("/add")
    public Result add(@RequestBody Share share) {
        if (share.getShare_id() == null || share.getShared_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        if (!share.getShare_id().equals(share.getShared_id())) {
//            Member member = memberService.findById(share.getShare_id());
            List<Share> shareList = shareService.selectShareListBySharedId(share.getShared_id());
            if (shareList.size() == 0) {
                share.setAdd_time(DateUtil.getNowTimestamp());
                shareService.save(share);
//                member.setMember_surplus_number(member.getMember_surplus_number() + 20);
//                memberService.update(member);
            }
        }
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        shareService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Share share) {
        shareService.update(share);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Share share = shareService.findById(id);
        return ResultGenerator.successResult(share);
    }

    @GetMapping("/list")
    public Result list(PageBean<Share> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Share> list = shareService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }
}
